package com.group3.merchant.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImageLoader extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ImageLoader() {
		super();

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		showImage(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		showImage(request, response);
	}

	private void showImage(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String path = request.getParameter("path");
		if (path != null) {
			File file = new File(path);
			if (!file.isFile()) {
				file = new File("d:/AMC/1.jpg");
			}
			InputStream fis = new FileInputStream(file);
			OutputStream os = response.getOutputStream();
			byte[] buff = new byte[4096];
			int len = 0;
			while ((len = fis.read(buff)) != -1) {
				os.write(buff, 0, len);
			}
			os.flush();
			os.close();
			fis.close();
		}
	}
}
